﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for YearEditorView.xaml
    /// </summary>
    public partial class YearEditorView
    {
        private readonly YearController _yearControl;
        private readonly Year _year;

        public YearEditorView()
        {
            InitializeComponent();

            _yearControl = new YearController();
            Title = "Add Year";
        }

        public YearEditorView(Year year)
        {
            InitializeComponent();
            _yearControl = new YearController();
            Title = "Edit Year";

            _year = year;

            textBoxId.Text = year.Id.ToString();
            textBoxNumber.Text = year.Number.ToString();
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxNumber.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxNumber.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                try
                {
                    var yearDigit = textBoxNumber.Text.ToCharArray();

                    if (yearDigit.Any(cell => !char.IsDigit(cell)))
                    {
                        throw new Exception();
                    }

                    if (Title.Equals("Add Year"))
                    {
                        var year = new Year
                                       {
                                           Number = Int32.Parse(textBoxNumber.Text)
                                       };

                        var message = _yearControl.AddYear(year);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses menambah year");
                                ClearTextBox();
                                break;
                            default:
                                MessageBox.Show("tahun sudah ada");
                                break;
                        }
                    }
                    else
                    {
                        var newYear = new Year
                        {
                            Id = _year.Id,
                            Number = Int32.Parse(textBoxNumber.Text)
                        };

                        var message = _yearControl.EditYear(newYear, _year.Number);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses mengedit year");
                                break;
                            default:
                                MessageBox.Show("tahun sudah ada");
                                break;
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("field number harus angka");
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
